//
//  CZComposeViewController.swift
//  GZWeibo666
//
//  Created by hyk on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

import SVProgressHUD

//枚举控制器类型
enum CZComposeViewControllerType:Int {
   case comment = 1
   case forward = 2

}
class CZComposeViewController: UIViewController {

    //MARK : 属性
    /// 微博模型
    var status: CZStatus?
     //设置控制器的类型
    var controllerType:CZComposeViewControllerType
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(controllerType :CZComposeViewControllerType){
     self.controllerType = controllerType
      super.init(nibName: nil, bundle: nil)
    
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置view的背景颜色
        view.backgroundColor = UIColor.brownColor()
        
        prepareUI()
    }
    
    //MARK:准备UI界面
    func prepareUI(){
     //设置导航栏
      setupNavigationBar()
     //设置文本信息
    setupTextView()
    }
    private func setupNavigationBar(){
    
      //左边的按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
      //右边的按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
    
      //调用标题导航栏的方法
        setupTitleView()
    }
    
    func setupTitleView(){
        //默认是发微博
        var prefix  = "发微博"
        if controllerType == CZComposeViewControllerType.forward {
           
           prefix = "转发微博"
        }
        if controllerType == CZComposeViewControllerType.comment {
        
         prefix = "评论微博"
        }
        
        //判断有没有用户名称
        if let name = CZUserAccount.loadUserAccount()?.screen_name{
          //有用户昵称
          let titlename = prefix + "\n" + name
        
           //给导航栏中间设置一个label来显示
            let label = UILabel()
            
            ///设置label的属性
            //行
            label.numberOfLines = 0
            //居中
            label.textAlignment = NSTextAlignment.Center
            //字体
            label.font = UIFont.systemFontOfSize(14)
            
            //因为用户名是可变的，所以设置为可变
            let textString = NSMutableAttributedString(string: titlename)
            
            // 获取NSRange
            let nameRange = (titlename as NSString).rangeOfString(name)
            
            // 设置属性文本的属性
            textString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: nameRange)
            textString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: nameRange)
            

            //设置label的文本
            label.attributedText = textString
            //尺寸适配
            label.sizeToFit()
            
            navigationItem.titleView = label

        }else {
         //没有用户昵称
          navigationItem.title = prefix
        }
        
    
    
    }
    //设置文本信息
    func setupTextView(){
     //添加控件
     view.addSubview(textView)
     //添加约束
     //填充满屏幕
    textView.ff_Fill(view)
      
    
    }
    //关闭控制器
    func cancel(){
    // 关闭sv提示
    //  SVProgressHUD.dismiss()
    dismissViewControllerAnimated(true, completion: nil )
    }
    //发送微博
    func sendStatus(){
       //获取编辑的文本信息到服务器
        let text = textView.text
        
        //判断文本为空
        if text == nil || text == "" {
         //弹框显示
            SVProgressHUD.showInfoWithStatus("内容不能为空")
          return
        }
        //转发微博
        if controllerType == CZComposeViewControllerType.forward {
          //弹框显示正在转发
            SVProgressHUD.showInfoWithStatus("正在玩命转发......", maskType: SVProgressHUDMaskType.Black)
            //获取网络信息
            CZNetworkTool.sharedInstance.forwarStatus(status!.id, status: text, finished: { (result, error) -> () in
                //有错误的时候
                if error != nil {
                  SVProgressHUD.showInfoWithStatus("网络不给力......", maskType: SVProgressHUDMaskType.Black)
                return
                }
                //发送成功,关闭窗口
                self.cancel()
            })
        }else if controllerType == CZComposeViewControllerType.comment{
           //评论微博
          
            //弹框显示正在评论
            SVProgressHUD.showInfoWithStatus("正在拼命评论中......", maskType: SVProgressHUDMaskType.Black)
            //获取网络信息
            CZNetworkTool.sharedInstance.commentStatus(status!.id, comment: text, finished: { (result, error) -> () in
                //有错误的时候
                if error != nil {
                    SVProgressHUD.showInfoWithStatus("网络不给力......", maskType: SVProgressHUDMaskType.Black)
                    return
                }
                //发送成功,关闭窗口
                self.cancel()
            })

        
        }
    
    }
    
    ///懒加载textView
    private lazy var textView: UITextView = {
     let view = UITextView()
        
     //text的字体大小
     view.font = UIFont.systemFontOfSize(18)
     //背景颜色
     view.backgroundColor = UIColor.brownColor()
     //字体颜色
     view.textColor = UIColor.blackColor()
     
    
    return view
    }()
}




